package flc.ast.fragment;

import static com.blankj.utilcode.util.ResourceUtils.getDrawable;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.TimeUtils;
import com.google.gson.Gson;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import flc.ast.R;
import flc.ast.activity.AppConfig;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private MyBannerAdapter bannerAdapter;
    private int selPosition = 0;
    private boolean isCollect = false;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivAnimationCollect.setOnClickListener(this);
        mDataBinding.tvAnimationDesc.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAnimationCollect:
                List<StkResBean> listSp = AppSpUtils.getCollectList();
                if (bannerAdapter.getData(selPosition).isSelected()) {
                    for (StkResBean stkResBean : listSp) {
                        if (stkResBean.equals(bannerAdapter.getData(selPosition))) {
                            listSp.remove(stkResBean);
                            break;
                        }
                    }
                    AppSpUtils.saveCollectList(listSp);
                    bannerAdapter.getData(selPosition).setSelected(false);
                    mDataBinding.ivAnimationCollect.setImageResource(R.drawable.aweishouc);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(bannerAdapter.getData(selPosition));
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    mDataBinding.ivAnimationCollect.setImageResource(R.drawable.ayishoucang);
                    bannerAdapter.getData(selPosition).setSelected(true);
                }
                break;
            case R.id.tvAnimationDesc:
                StkResBean bean=bannerAdapter.getData(selPosition);
                BaseWebviewActivity.open(mContext,bean.getUrl(),bean.getName());
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.HOME_DATA, StkApi.createParamMap(0, 10), true,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                for (StkResBean stkResBean : result) {
                                    List<StkResBean> listSp = AppSpUtils.getCollectList();
                                    if (listSp != null && listSp.size() != 0) {
                                        for (StkResBean bean : listSp) {
                                            if (bean.equals(stkResBean)) {
                                                stkResBean.setSelected(true);
                                                break;
                                            }
                                        }
                                    } else {
                                        break;
                                    }
                                }
                                bannerAdapter = new MyBannerAdapter(result, getContext());
                                mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
                                mDataBinding.homeBanner.setBannerGalleryEffect(100, 24);//40两边露出距离，24图片之间距离
                                mDataBinding.homeBanner.isAutoLoop(false);//false不自动轮播
                                mDataBinding.homeBanner.addOnPageChangeListener(new OnPageChangeListener() {
                                    @Override
                                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                                    }

                                    @Override
                                    public void onPageSelected(int position) {
                                        selPosition = position;
                                        setData(bannerAdapter.getData(position));
                                    }

                                    @Override
                                    public void onPageScrollStateChanged(int state) {

                                    }
                                });
                                setData(result.get(0));
                            }
                        }
                    }
                });
    }

    private void setData(StkResBean bean) {
        mDataBinding.tvAnimationName.setText(bean.getName());
        // 设置文本及其颜色
        StringBuffer str = new StringBuffer();
        str.append(bean.getDesc());
        str.append("<font color='#FB54FE'>" + getString(R.string.see_more) + "</font>");
        // 在文本控件中显示
        mDataBinding.tvAnimationDesc.setText(Html.fromHtml(str.toString(), getImageGetter(), null));
        if (bean.isSelected()) {
            mDataBinding.ivAnimationCollect.setImageResource(R.drawable.ayishoucang);
        } else {
            mDataBinding.ivAnimationCollect.setImageResource(R.drawable.aweishouc);
        }
        Random random=new Random();
        double number=random.nextInt(5);
        double score=4.5+number/10;
        mDataBinding.tvAnimationScore.setText(score+"");
    }

    private Html.ImageGetter getImageGetter() {
        return source -> {
            Drawable drawable = getDrawable(Integer.parseInt(source));
            drawable.setBounds(0, 0, 60, 60);
            return drawable;
        };
    }


}